"use strict";
const common_vendor = require("../common/vendor.js");
const config_app = require("../config/app.js");
const api_index = require("../api/index.js");
const api_user = require("../api/user.js");
const stores_userInfo = require("../stores/userInfo.js");
const userInfoStore = stores_userInfo.useUserInfoStore();
const EncryptTel = (phone) => {
  let str = "";
  if (phone) {
    str = phone.replace(/(\d{3})\d*(\d{4})/, "$1****$2");
  }
  return str;
};
const callPhone = (phoneNumber) => {
  if (phoneNumber) {
    common_vendor.index.makePhoneCall({
      phoneNumber
    });
  }
};
const onPageJump = (pageUrl) => {
  common_vendor.index.navigateTo({
    url: pageUrl
  });
};
const uploadFilePromise = (url) => {
  return new Promise((resolve, reject) => {
    common_vendor.index.uploadFile({
      url: config_app.https + "/common/upload",
      filePath: url,
      name: "file",
      header: {
        Authorization: "Bearer " + userInfoStore.token
      },
      success: (res) => {
        let data = JSON.parse(res.data);
        if (data.code === 200) {
          resolve(data);
        }
      }
    });
  });
};
const toFixedWithoutTrailingZeros = (num) => {
  let result = num.toFixed(2);
  result = result.replace(/\.00$/, "");
  result = result.replace(/\.0$/, "");
  return result;
};
const onLogin = (e, uToastRef) => {
  common_vendor.index.login({
    provider: "weixin",
    success: function(res) {
      if (res.code) {
        const { code } = res;
        common_vendor.index.showLoading({
          title: "登录中……"
        });
        api_index.getWxOpenId({ code }).then((res1) => {
          common_vendor.index.hideLoading();
          userInfoStore.setOpenId(res1.data);
          if (e.detail.errMsg == "getPhoneNumber:ok") {
            getUserInfo(res1.data, e.detail.code, uToastRef);
          } else {
            uToastRef.value.show({
              type: "warning",
              message: "授权失败！"
            });
          }
        }).catch((err) => {
          uToastRef.value.show({
            type: "error",
            message: err
          });
        });
      } else {
        uToastRef.value.show({
          type: "warning",
          message: "登录失败！"
        });
      }
    }
  });
};
const getUserInfo = (openid, code, uToastRef) => {
  api_index.userLogin({ code, openid }).then(async (res) => {
    const { data } = res;
    userInfoStore.setToken(data.token);
    data.avatar = config_app.imgPrefix + data.avatar;
    userInfoStore.setUserInfo(data);
    if (data.userType == 2) {
      common_vendor.index.redirectTo({
        url: "/pages/police/home/index"
      });
    } else if (data.userType == 3) {
      common_vendor.index.redirectTo({
        url: "/pages/leader/home/index"
      });
    } else if (data.userType == 1) {
      if (userInfoStore.userInfo.registerStatus == 1) {
        await getCrowdUserInfo(uToastRef);
        await common_vendor.index.redirectTo({
          url: "/pages/crowd/home/index"
        });
      } else {
        common_vendor.index.navigateTo({
          url: "/pages/register/register"
        });
      }
    } else {
      common_vendor.index.navigateTo({
        url: "/pages/register/register"
      });
    }
  }).catch((err) => {
    uToastRef.value.show({
      type: "error",
      message: err
    });
  });
};
const getCrowdUserInfo = async (uToastRef) => {
  api_user.crowdUserInfo({ crowdUserId: userInfoStore.userInfo.userId }).then((res) => {
    const { data } = res;
    userInfoStore.setCrowdInfo(data);
  }).catch((err) => {
    uToastRef.value.show({
      type: "error",
      message: err
    });
  });
};
exports.EncryptTel = EncryptTel;
exports.callPhone = callPhone;
exports.getCrowdUserInfo = getCrowdUserInfo;
exports.onLogin = onLogin;
exports.onPageJump = onPageJump;
exports.toFixedWithoutTrailingZeros = toFixedWithoutTrailingZeros;
exports.uploadFilePromise = uploadFilePromise;
